import * as _ from 'lodash';
import { resolveReflectiveProviders } from '@angular/core/src/di/reflective_provider';

export class ProcedureService {
    datas: Array<any> = [
        {
            id: '001',
            name: '云主机审批流程',
            processKey: 'procNormal',
            status: "启用",
            comment: "云主机一般审批流程",
            createTime: '2018-04-13',
            creator: '管理员'

        },
        {
            id: '002',
            name: '云主机特殊审批流程',
            processKey: 'procSpec',
            status: "启用",
            comment: "云主机特殊审批流程",
            createTime: '2018-04-14',
            creator: '管理员'
        },
        {
            id: '004',
            name: '云存储特殊审批流程',
            processKey: 'procSpec2',
            status: "停用",
            comment: "云存储特殊审批流程",
            createTime: '2018-04-14',
            creator: '管理员'
        }
    ];


    constructor() {

    }

    queryMachine(condition) {
        let list = _.cloneDeep(this.datas);
        let result = [];
        let findResult = [];
        if (!condition) {
            return list;
        }
        let search = condition.search;
        if (search != null) {
            for (let l of list) {
                if (l.name.indexOf(search) >= 0) {
                    result.push(l);
                }
            }
        } else {
            result = list;
        }

        if (condition.sort) {
            result = _.orderBy(result, [
                condition.sort.field
            ], [condition.sort.sort]);
        }

        let pageSize = condition.pageSize;
        if (pageSize == null) {
            pageSize = 10;
        }

        let pageNo = condition.pageNo;
        if (pageNo == null) {
            pageNo = 1;
        }

        for (let i = 0; i < result.length; i++) {
            if (i >= (pageNo - 1) * pageSize) {
                findResult.push(result[i]);
            }
            if (findResult.length >= pageSize) {
                break;
            }
        }

        return {
            data: findResult,
            totalCount: result.length
        };
    }

}